import uuid
import time
from selenium import webdriver
from selenium自动化测试.组件化三.loctor import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
"""
元素放到元素页面里
执行放到执行页面里---》执行页面调用元素页面

这么做的目的：
为了后期维护成本低一点，元素发生了改变，只需要修改存储元素的页面就可以了
"""

class CRMEB:
    uuid_ = str(uuid.uuid4()).split('-')[0]  # 50e2f5a9
    def __init__(self,driver,url):
        self.driver = driver
        # 初始化驱动
        self.driver = webdriver.Chrome()
        # 打开网页
        self.driver.get(url)
        # 设置隐式等待
        self.driver.implicitly_wait(15)
        # 最大化窗口
        self.driver.maximize_window()
    def login(self,user:str,password:str):
        # 输入账号：
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_ele))[0].send_keys(user)
        # 输入密码：
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(password_ele))[1].send_keys(password)
        # 点击登录
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(login_ele)).click()
    def add_user_group(self,name):
        self.name = name
        # 点击用户：
        time.sleep(3)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_icon_ele))[1].click()
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(user_group_ele)).click()
        # 点击添加
        time.sleep(2)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(add_icon_ele)).click()
        # 输入分组名称
        time.sleep(1)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(input_user_group_ele))[2].send_keys(self.name+str(self.uuid_))
        # 点击确定
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(click_accept_ele))[2].click()
    def check_name(self):
        time.sleep(2)
        text = self.driver.find_element(By.XPATH, f"//*[text()='{self.name}{str(self.uuid_)}']").text
        assert text == self.name + str(self.uuid_)
        time.sleep(15)
if __name__ == '__main__':
     driver = webdriver.Chrome()
     url = "http://121.40.86.4:8899/admin"
     # 实例化对象
     d_crmeb = CRMEB(driver,url)
     # 进行登录
     d_crmeb.login('admin','Aa010903!')
     # 新增用户分组
     d_crmeb.add_user_group("社会琳")
     # 检查是否新增成功
     d_crmeb.check_name()




